package edu.neu.cs6515.xml.converter;

import javax.swing.plaf.basic.BasicSliderUI.ActionScroller;
import javax.xml.bind.JAXBContext;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import edu.neu.cs6515.xml.w12.AdministratorResponses;
import edu.neu.cs6515.xml.w12.PlayerActions;
import edu.neu.cs6515.xml.w12.TurnTest;
import edu.neu.cs6515.xml.w12.XMLTurn;

public class RemoteTurnConverter implements Converter {

    @Override
    public boolean canConvert(final Class type) {
        return TurnTest.class.equals(type);
    }

    @Override
    public void marshal(final Object source,
            final HierarchicalStreamWriter writer, final MarshallingContext context) {
        TurnTest turnTest = (TurnTest) source;
        XMLTurn turn = turnTest.getTurn();
        writer.startNode("turn");
        context.convertAnother(turn);
        writer.endNode();
        AdministratorResponses ac = turnTest.getAdministrator();
        writer.startNode("administrator");
        context.convertAnother(ac);
        writer.endNode();
        PlayerActions pa = turnTest.getActions();
        writer.startNode("actions");
        context.convertAnother(pa);
        writer.endNode();
    }

    @Override
    public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
        TurnTest turnTest = new TurnTest();
        reader.moveDown();
        XMLTurn turn = (XMLTurn) context.convertAnother(turnTest,
                XMLTurn.class);
        turnTest.setTurn(turn);
        reader.moveUp();

        reader.moveDown();
        AdministratorResponses ac = (AdministratorResponses) context.convertAnother(turnTest,
                AdministratorResponses.class);
        turnTest.setAdministrator(ac);
        reader.moveUp();

        reader.moveDown();
        PlayerActions pa = (PlayerActions) context.convertAnother(turnTest,
                PlayerActions.class);
        turnTest.setActions(pa);
        reader.moveUp();
        return turnTest;
    }
}